﻿using AnesSystem.Models;
using AnesSystem.Util;
using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{

    class DepartmentCountCtrlViewModel : BaseCountCtrlViewModel<DepartmentCountCtrlViewModel>
    {
        #region Variables
        #endregion

        #region Constructor
        public DepartmentCountCtrlViewModel()
        { }



        #endregion

        #region Properties




        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        #endregion

        #region Public/Protected Methods
        public void Excel()
        {

            if (PatientLinkModelList == null || PatientLinkModelList.Count == 0)
            {
                MessageBoxCommon.ShowDialog("请先查询病人!", MessageBoxCommon.OK);
                return;
            }
            if (DisPatientLinkModelList == null || DisPatientLinkModelList.Count == 0)
            {
                MessageBoxCommon.ShowDialog("请先选择病人集合!", MessageBoxCommon.OK);
                return;
            }
            ExportToExcel excel = new ExportToExcel();
            excel.ExportParameterCommon("历史手术", DisPatientLinkModelList.ToList());
        }

        public void UpdateDisPatientLinkModelList(BaseCountModel SelBaseCountModel)
        {
            if (SelBaseCountModel == null) return;
            string name = "";
            if ("空".Equals(SelBaseCountModel.Name))
                DisPatientLinkModelList = PatientLinkModelList.Where(O =>
                {
                    if (string.IsNullOrEmpty(O.Patient.Ksmc))
                        return true;
                    return false;

                }).ToList();
            if (SelBaseCountModel.Name != null)
            {               
                DisPatientLinkModelList.Clear();
                DisPatientLinkModelList = PatientLinkModelList.Where(O =>
                {
                    name = "";
                    if (!string.IsNullOrEmpty(O.Patient.Ksmc))
                    {                        
                        name = O.Patient.Ksmc;
                        if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_SC_SCDC)) {
                            name = name.Replace("护理单元", "");
                            name = name.Replace("病区", "");
                        }
                        return name.Equals(SelBaseCountModel.Name);
                    }                        
                    else
                        return false;
                }).ToList();
            }
        }

        public override void UpdateBaseCountModelList()
        {
            //Patient.Ksmc
            foreach (PatientLinkModel patientLinkModel in PatientLinkModelList)
            {
                bool onOff = true;
                if (patientLinkModel.Patient.Ksmc != null)
                {
                    string name = "";
                    //获取左边显示的科室名称
                    if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_SC_SCDC))
                    {
                        //if (!string.IsNullOrEmpty(patientLinkModel.Patient.Ksbh))
                        //{
                        //    ConfigDepartmentsModel department = DataManager.Instance.ConfigDepartmentsModelList.Where(O => O.Code.Equals(patientLinkModel.Patient.Ksbh)).ToList().First();
                        //    name = department.Name;
                        //}
                        //else
                        //{
                        //    name = patientLinkModel.Patient.Ksmc;
                        //}
                        name = patientLinkModel.Patient.Ksmc;
                        name = name.Replace("护理单元", "");
                        name = name.Replace("病区", "");
                    }
                    else
                    {
                        name = patientLinkModel.Patient.Ksmc;
                    }
                    if (string.IsNullOrEmpty(name)) name = "空";
                    foreach (BaseCountModel baseCountModel in BaseCountModelList)
                    {
                        if (baseCountModel.Name.Equals(name))
                        {
                            onOff = false;
                            baseCountModel.Count++;
                        }
                    }
                    if (onOff && !string.IsNullOrEmpty(name))
                    {
                        BaseCountModel baseCountModel = new BaseCountModel();
                        baseCountModel.Name = name;
                        baseCountModel.Count = 1;
                        BaseCountModelList.Add(baseCountModel);
                    }
                }
            }

        }

        #endregion

        #region EventHandlers

        #endregion


    }
}
